package com.ldatum.crm.daoImpl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

import com.ldatum.crm.dao.TTracksDao;
import com.ldatum.crm.domain.NewTracks;
import com.ldatum.crm.domain.TTracks;
import com.ldatum.crm.util.HibernateUtil;

public class TTracksDaoImpl implements TTracksDao {

	@Override
	public TTracks findById(Class<TTracks> class1, int id) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			TTracks tracks = (TTracks) s.get(class1, id);
			Hibernate.initialize(tracks.getCustomer());
			return tracks;
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public void save(TTracks tracks) {
		HibernateUtil.add(tracks);
	}
	

	@Override
	public void delete(TTracks tracks, int id) throws Exception {
		HibernateUtil.delete(tracks);
	}

	@Override
	public void update(TTracks tracks, int id) throws Exception {
		HibernateUtil.update(tracks);
	}

	@Override
	public List<TTracks> list(Class<TTracks> clazz, String employeeName)
			throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
//			String hql = "select t from TTracks as t ,TCustomer as c where t.id = c.id and c.proprietor=:proprietor";
//			String hql = "select t.* from t_tracks as t ,t_customer as c where t.customer_id = c.id and c.proprietor=:proprietor";
//			Query q = s.createSQLQuery(hql).addEntity(TTracks.class);
//			q.setString("proprietor", employeeName);
			/**
			 * 采用原生的sql语句执行
			 
			String sql = "select t.*,c.short_name as customerName from t_tracks as t ,t_customer as c "
					+ "where t.customer_id = c.id and c.proprietor=:proprietor";
			Query q1 = s.createSQLQuery(sql).addEntity(TTracks.class);
			q1.setString("proprietor", employeeName);
			@SuppressWarnings("unchecked")
			List<TTracks> list = (List<TTracks>) q1.list();
			*/
			
			/**
			 *采用hql的方式 
			 *采用hql方式的时候注意：在TTrack模型中要有customer_id这个属性，否则会报错。
			
			String hql = "select t from TTracks as t ,TCustomer as c where t.Customer = c.id and c.proprietor=:proprietor";
			Query q = s.createQuery(hql);
			q.setString("proprietor", employeeName);
			@SuppressWarnings("unchecked")
			List<TTracks> list = (List<TTracks>) q.list();
			//懒加载处理
//			for(TTracks con:list){
//				Hibernate.initialize(con.getCustomer());
//			}
			 */
			
			String hql = "from TTracks as tracks where tracker like :tracker order by create_date_time desc";
			Query q = s.createQuery(hql);
			q.setString("tracker", employeeName);
			@SuppressWarnings("unchecked")
			List<TTracks> list = (List<TTracks>) q.list();
			//懒加载处理
			for(TTracks track:list){
				Hibernate.initialize(track.getCustomer());
			}
			return list;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
	
	@Override
	public List<NewTracks> list1(Class<NewTracks> clazz, String employeeName)
			throws Exception {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String sql = "select new NewTracks(t,c.short_name as customerName) from TTracks as t ,TCustomer as c "
					+ "where t.customer_id = c.id and c.proprietor=:proprietor";
			Query q1 = s.createQuery(sql);
			q1.setString("proprietor", employeeName);
			@SuppressWarnings("unchecked")
			List<NewTracks> list = (List<NewTracks>) q1.list();
			return list;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}

}
